home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 15 / BBS in a box XV-1.iso / Files / Internet / Misc / Uupc 3.1 sources.sit / uupc 3.1 sources Folder / (uupc π) / scandir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-01  |  2.5 KB  |  103 lines  |  [TEXT/KAHL]

  1. /*
  2.  *    scandir - a SysV library routine that simulates the
  3.  *          BSD 4.3 version.
  4.  *
  5.  *    returns:
  6.  *        -1 for failures
  7.  *        # of files found if successful
  8.  *
  9.  *    Produces a list of files which are contained in the
  10.  *    directory "dirname".
  11.  *
  12.  *    The list is returned through "namelist" which is an
  13.  *    array of pointers to (struct direct).  This list of
  14.  *    pointers are malloc'ed and should be freed when
  15.  *    done with.
  16.  *
  17.  *    The passed function "select" is passed a (struct direct)
  18.  *    pointer for each directory entry.  If "select" returns
  19.  *    non-zero the entry is included in "namelist", otherwise
  20.  *    it is not.  If "select" is NULL, all entries are returned.
  21.  *
  22.  *    The function "compar" is passed a pair of pointers to
  23.  *    (struct direct) and is used to sort "namelist" via qsort.
  24.  *    If "compar" is NULL, the names are unsorted.
  25.  *
  26.  *    This source is public domain and the author claims no
  27.  *    rights to it.  It may be copied, spindled, or mutilated.
  28.  *
  29.  *    R.J. Esposito - Bell of Pennsylvania
  30.  *
  31.  */
  32.  
  33. #include <stdio.h>
  34. /*#include <sys/types.h>*/
  35. #include "ndir.h"
  36.  
  37. #include "scandir.proto.h"
  38.  
  39. int
  40. scandir(char *dirname, struct direct *(*namelist[]), int (*select )(), int (*compar )())
  41. {
  42.     DIR        *dfp;
  43.     struct direct    *dp;
  44.     register int    ii, nf;
  45.     char        *malloc();
  46.  
  47.     if ((dfp = opendir(dirname)) == NULL)    /* can't open directory */
  48.         return(-1);
  49.  
  50.     nf = 0;
  51.     while ((dp = readdir(dfp)) != NULL)    /* read thru direcetory */
  52.         if (select == NULL || (*select)(dp))
  53.             nf++;
  54.  
  55.     if (!nf)                /* nothing found */
  56.         return(0);
  57.  
  58.     /* malloc memory for the namelist array */
  59.  
  60.     *namelist = (struct direct **)malloc((nf+1)*sizeof(struct direct *));
  61.     if (*namelist == NULL) {
  62.         fprintf(stderr, "scandir: out of memory\n");
  63.         return(-1);
  64.     }
  65.  
  66.     for (ii = 0; ii < nf; ii++) {
  67.         (*namelist)[ii] = (struct direct *)malloc(sizeof(struct direct));
  68.         if ((*namelist)[ii] == NULL) {
  69.             fprintf(stderr, "scandir: out of memory\n");
  70.             return(-1);
  71.         }
  72.     }
  73.  
  74.     /* now re-read the directory loading up the namelist array */
  75.  
  76.     closedir( dfp );
  77.     if ((dfp = opendir(dirname)) == NULL)    /* can't open directory */
  78.         return(-1);
  79.  
  80.     (*namelist)[ii] = 0;
  81.     /*seekdir(dfp, 0L);*/
  82.     ii = 0;
  83.  
  84.     while ((dp = readdir(dfp)) != NULL) {
  85.         if (select == NULL || (*select)(dp)) {
  86.             (*namelist)[ii]->d_ino = dp->d_ino;
  87.             (*namelist)[ii]->d_reclen = dp->d_reclen;
  88.             (*namelist)[ii]->d_namlen = dp->d_namlen;
  89.             strcpy((*namelist)[ii]->d_name, dp->d_name);
  90.             ii++;
  91.         }
  92.     }
  93.  
  94.     closedir(dfp);
  95. #ifdef test
  96.     if (compar != NULL)        /* sort the list if required */
  97.         qsort((char **)*namelist, nf, sizeof(struct direct *), compar);
  98. #endif
  99.  
  100.     return(nf);
  101. }
  102.  
  103.